package com.tensequare.web.filter;

import com.netflix.client.http.HttpRequest;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: Feng.Wang
 * @Company: Zelin.ShenZhen
 * @Description:
 * @Date: Create in 2019/3/30 15:23
 */
@Component
public class WebFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return "pre";
    }
    //这里代表执行的过滤器的顺序，数值越小越先被调用
    @Override
    public int filterOrder() {
        return 0;
    }
    //是否启用过滤器 true:代表启用 false：代表不启用
    @Override
    public boolean shouldFilter() {
        return true;
    }
    //过滤器执行的业务代码
    //只要返回的为对象（即使为null也要执行），就回被调用向下执行
    @Override
    public Object run() throws ZuulException {
        //1.得到当前请求的上下文对象
        RequestContext currentContext = RequestContext.getCurrentContext();
        //2.得到请求对象
        HttpServletRequest request = currentContext.getRequest();
        //3.得到请求头
        String header = request.getHeader("Authorization");
        //4.如果请求头有内容
        if(null != header) {
            currentContext.addZuulRequestHeader("Authorization", header);
        }
        return null;
    }
}
